package com.cuit.xsc.weishare.activity;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cuit.xsc.weishare.R;
import com.cuit.xsc.weishare.adapter.PrivateMsgConversationAdapter;
import com.cuit.xsc.weishare.logic.MainService;
import com.cuit.xsc.weishare.logic.Task;
import com.cuit.xsc.weishare.model.DirectMessage;
import com.cuit.xsc.weishare.model.PrivateMsgResult;
import com.cuit.xsc.weishare.model.Statuse;
import com.cuit.xsc.weishare.util.DirectMessageComparator;
import com.cuit.xsc.weishare.util.LogUtil;
import com.cuit.xsc.weishare.util.MessageDialog;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class PrivateMsgConversationAct extends BaseActivity implements OnClickListener
{
	private TextView titleTv;
	private View progresView;
	private String currentUserNick;
	private String currentUserName;
	private ListView mListView;
	private List<DirectMessage> dList;
	private PrivateMsgConversationAdapter adapter;
	
	private EditText editText;
	private Button sendBut;
	private MessageDialog dialog = null;
	
	private DirectMessage recvDirectMessage = null;
	private DirectMessage sendDirectMessage = null;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conversation);
		dList = (List<DirectMessage>) getIntent().getSerializableExtra("list");
		LogUtil.i(this, "dList="+dList);
	
		
		titleTv = (TextView) findViewById(R.id.txt_wb_title);
		
		editText = (EditText) findViewById(R.id.converstation_msg);
		sendBut = (Button) findViewById(R.id.converstation_send);
		sendBut.setOnClickListener(this);
		
		progresView = this.findViewById(R.id.layout_progress);
		progresView.setVisibility(View.GONE);
		
		mListView = (ListView) findViewById(R.id.converstation_list);
		adapter = new PrivateMsgConversationAdapter(this);
		if (dList != null && !dList.isEmpty())
		{
			Collections.sort(dList, new DirectMessageComparator());
			DirectMessage message = dList.get(0);
			if (message.getType() == DirectMessage.RECV)
			{
				currentUserNick = message.getNick();
				currentUserName = message.getName();
			}
			else
			{
				currentUserNick = message.getTonick();
				currentUserName = message.getToname();
			}
			
			for (DirectMessage dMessage : dList)
			{
				if (dMessage.getType() == DirectMessage.RECV)
				{
					recvDirectMessage = dMessage;
				}
				
				if (dMessage.getType() == DirectMessage.SEND)
				{
					sendDirectMessage = dMessage;
				}
			}
			
			
			
			adapter.setList(dList);
			adapter.setListView(mListView);
			mListView.setAdapter(adapter);
		}
		
		titleTv.setText(currentUserNick);
		LogUtil.i(this, "listView="+mListView);
		dialog = new MessageDialog(this);
	}
	
	@Override
	public void refresh(Object... param)
	{
		//super.refresh(param);
		LogUtil.i(this, "hello");
		dismissProgress();
		int flag = ((Integer) param[0]).intValue();
		switch (flag)
		{
		case Task.ADD_PRIVATE_MSG:
			PrivateMsgResult msgResult =  (PrivateMsgResult) param[1];
			if (msgResult != null)
			{
				System.out.println(msgResult.getId()+"_"+msgResult.getTime());
				dialog.ShowToast(msgResult.getId()+"_"+msgResult.getTime());
				addMessage(msgResult);
				
			}
			else
			{				
				dialog.ShowToast("发送失败，请稍后再试！");
			}
		break;
		}
		
		
	}
	
	
	private void addMessage(PrivateMsgResult msgResult)
	{
		DirectMessage directMessage = new DirectMessage(DirectMessage.SEND);
		directMessage.setId(msgResult.getId());
		directMessage.setTime(msgResult.getTime());
		directMessage.setText(editText.getText().toString());
		if (sendDirectMessage != null)
		{
			directMessage.setName(sendDirectMessage.getName());
			directMessage.setNick(sendDirectMessage.getNick());
			directMessage.setHead(sendDirectMessage.getHead());
			
			directMessage.setToname(sendDirectMessage.getToname());
			directMessage.setTonick(sendDirectMessage.getTonick());
			directMessage.setTohead(sendDirectMessage.getTohead());
			
		}
		else if (recvDirectMessage != null) 
		{
			directMessage.setToname(sendDirectMessage.getName());
			directMessage.setTonick(sendDirectMessage.getTonick());
			directMessage.setTohead(sendDirectMessage.getTohead());
		}
		editText.setText("");
		if (dList != null)
		{
			dList.add(directMessage);
			adapter.notifyDataSetChanged();
		}
		
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.converstation_send:
			String content = editText.getText().toString();
			System.out.println("content="+content);
			if (content == null || "".equals(content) )
			{
				dialog.ShowToast("内容不能为空！");
				return;
			}
			
			
			if (currentUserName != null)
			{
				Map map = new HashMap();
				map.put("content", content);
				map.put("name", currentUserName);
				
				Task task = new Task(Task.ADD_PRIVATE_MSG, map);
				MainService.addTask(task);
				//progresView.setVisibility(View.VISIBLE);
				
				showProgress();
				
			}
		
			break;

		default:
			break;
		}
	}   

}
